<?php

/**
 * Created by PhpStorm.
 * User: 技术员专用
 * Date: 2019/3/11
 * Time: 16:26
 */

$http = new swoole_http_server('0.0.0.0', 8811);
$http->set([
    'document_root' => '/www/wwwroot/live/tp5/public/static',
    'enable_static_handler' => true,
]);

$http->on('WorkerStart', function (swoole_server $server, $worker_id) {
    define('APP_PATH', __DIR__ . '/../application/');
    require __DIR__ . '/../thinkphp/base.php';
});
$http->on('request', function ($request, $response) use ($http) {
    $_SERVER=[];
    $_GET=[];
    $_POST=[];
    if (isset($request->server)) {
        foreach ($request->server as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }
    if (isset($request->header)) {
        foreach ($request->header as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }
    if (isset($request->get)) {
        foreach ($request->get as $k => $v) {
            $_GET[$k] = $v;
        }
    }
    if (isset($request->post)) {
        foreach ($request->post as $k => $v) {
            $_POST[$k] = $v;
        }
    }
    ob_start();
    // 执行应用并响应
    try{
        think\Container::get('app',[APP_PATH])->run()->send();
    }catch (Exception $e){
        //todo
    }
    $res = ob_get_contents();
    if(ob_get_contents()) ob_end_clean();
    $response->end($res);
//    $http->close();
//    $response->cookie('cok', '1111', time() + 60);//设置cookie，需要在end之前
//    $response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>");

});
$http->start();